{% extends 'index.html' %}

{% block content %}

<!-- 右侧详细内容 -->
<div class="col-md-9">
    <h1 class="mt-4">新建定时任务</h1>
    <form method="POST">
        <div class="mb-3">
            <label for="task_name" class="form-label">任务名称</label>
            <input type="text" class="form-control" id="task_name" name="task_name" required>
        </div>
        <div class="mb-3">
            <label for="config_ids" class="form-label">选择配置文件</label>
            <select class="form-select" id="config_ids" name="config_ids" multiple="multiple" required>
                {% for config in configs %}
                    <option value="{{ config[0] }}">
                        {{ config[1] }} (ID: {{ config[0] }})
                    </option>
                {% endfor %}
            </select>
        </div>
        <div class="mb-3">
            <label for="interval_type" class="form-label">时间间隔类型</label>
            <select class="form-select" id="interval_type" name="interval_type" required>
                <option value="minute">每分钟</option>
                <option value="hourly">每小时</option>
                <option value="daily">每日</option>
                <option value="weekly">每周</option>
                <option value="monthly">每月</option>
            </select>
        </div>
        <div class="mb-3">
            <label for="interval_value" class="form-label">时间间隔值</label>
            <input type="number" class="form-control" id="interval_value" name="interval_value" required>
            <small id="intervalHelp" class="form-text text-muted">请输入有效的间隔值。</small>
        </div>
        <div class="mb-3">
            <label for="task_mode" class="form-label">任务模式</label>
            <select class="form-select" id="task_mode" name="task_mode" required>
                <option value="strm_creation">运行 STRM 创建</option>
                <option value="strm_validation_quick">STRM 文件有效性检测（快扫）</option>
                <option value="strm_validation_slow">STRM 文件有效性检测（慢扫）</option>
            </select>
        </div>
        <div class="mb-3">
            <label for="is_enabled" class="form-label">是否启用</label>
            <select class="form-select" id="is_enabled" name="is_enabled" required>
                <option value="1">启用</option>
                <option value="0">禁用</option>
            </select>
        </div>
        <button type="submit" class="btn btn-primary">保存任务</button>
    </form>
</div>

<!-- 引入 Select2 CSS 和 JS -->
<link href="{{ url_for('static', filename='css/select2.min.css') }}" rel="stylesheet">
<script src="{{ url_for('static', filename='js/jquery-3.6.0.min.js') }}"></script>
<script src="{{ url_for('static', filename='js/select2.min.js') }}"></script>

<script>
    $(document).ready(function() {
        $('#config_ids').select2({
            placeholder: "请选择配置文件",
            allowClear: true
        });

        function updateIntervalValueRange() {
            var intervalValueInput = document.getElementById('interval_value');
            var intervalHelp = document.getElementById('intervalHelp');
            var intervalType = document.getElementById('interval_type').value;
            if (intervalType == 'minute') {
                intervalValueInput.min = 1;
                intervalValueInput.max = 59;
                intervalHelp.textContent = '分钟间隔值必须在 1 到 59 之间。';
            } else if (intervalType == 'hourly') {
                intervalValueInput.min = 1;
                intervalValueInput.max = 23;
                intervalHelp.textContent = '小时间隔值必须在 1 到 23 之间。';
            } else if (intervalType == 'daily') {
                intervalValueInput.min = 1;
                intervalValueInput.max = 31;
                intervalHelp.textContent = '天数间隔值必须在 1 到 31 之间。';
            } else if (intervalType == 'weekly') {
                intervalValueInput.min = 0;
                intervalValueInput.max = 6;
                intervalHelp.textContent = '星期值必须在 0（周日）到 6（周六）之间。';
            } else if (intervalType == 'monthly') {
                intervalValueInput.min = 1;
                intervalValueInput.max = 12;
                intervalHelp.textContent = '月份间隔值必须在 1 到 12 之间。';
            }
        }

        document.getElementById('interval_type').addEventListener('change', updateIntervalValueRange);
        // 页面加载时调用一次
        updateIntervalValueRange();
    });
</script>

{% endblock %}
